package com.zyy.handler;

import com.zyy.service.JSONSerializer;
import com.zyy.service.RpcDecoder;
import com.zyy.service.RpcEncoder;
import com.zyy.service.RpcRequest;
import io.netty.channel.Channel;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.handler.codec.string.StringDecoder;
import io.netty.handler.codec.string.StringEncoder;

/**
 * @author zyy
 * @date 2021/2/23 下午2:52
 * @description
 */
public class UserClientInitializer extends ChannelInitializer {
    private UserClientHandler userClientHandler;

    public UserClientInitializer(UserClientHandler userClientHandler) {
        this.userClientHandler = userClientHandler;
    }

    @Override
    protected void initChannel(Channel channel) throws Exception {
        //获取管道对象
        ChannelPipeline pipeline = channel.pipeline();
        //设置编码
        pipeline.addLast(new RpcEncoder(RpcRequest.class,new JSONSerializer()));
        pipeline.addLast(new StringDecoder());
        //添加自定义事件处理器
        pipeline.addLast(userClientHandler);

    }
}
